'use strict';

define(['angular', 'angular-ui-router', 'angular-md5', '../../../common/utils/service', 'readable-range', '../../login/login'], function (angular) {
	var module = angular.module('memberChangePassword', ['ui.router', 'dataService', 'login', 'angular-md5']);
	module.config(['$stateProvider', function ($stateProvider) {
		$stateProvider
			.state("member.changepassword", {
				url: '/change-password',
				views: {
					"@": {
						controller: 'MemberChangePasswordCtrl',
						templateUrl: '/mstore/member/change-password/change-password.html'
					}
				}
			})
	}]);
	function DialogController($scope, $mdDialog) {
		$scope.hide = function () {
			$mdDialog.hide();
		};
		$scope.cancel = function () {
			$mdDialog.cancel();
		};
		$scope.answer = function () {
			$mdDialog.hide();
		};
	}

	module.controller('MemberChangePasswordCtrl', ['$rootScope', '$state', '$scope', 'dataService', '$mdDialog', 'md5', function ($rootScope, $state, $scope, dataService, $mdDialog, md5) {
		$rootScope.pageTitle = "会员修改密码";
		$scope.memberBound(function () {
			$scope.changePassword = function (pwd, okPwd) {
				if (!pwd) {
					return $rootScope.showMessage("密码必须为6位0-9数字");
				}
				if (pwd != okPwd) {
					return $rootScope.showMessage("两次输入密码不一致");
				}


				//$mdDialog.show({
				//	controller: DialogController,
				//	templateUrl: 'mstore/member/change-password/dialog.html'
				//})
				//	.then(function () {
				//		$scope.changePasswordOk(pwd);
				//		$mdDialog.cancel();
				//	}, function () {
				//		$mdDialog.cancel();
				//	});


				var confirm = $mdDialog.confirm({timeout: 50})
					.title('修改密码')
					.content('确认修改会员支付密码？')
					.ok('确定')
					.cancel('取消');
				$mdDialog.show(confirm).then(function () {
					$scope.changePasswordOk(pwd);
					$mdDialog.cancel();
				}, function () {
					$mdDialog.cancel();
				});
			}

			$scope.changePasswordOk = function (pwd) {
				dataService.changePassword(md5.createHash(pwd), sessionStorage.memberId, function (err, data) {
					if (err) {
						console.log(err);
						console.log(data);
						return $rootScope.showMessage(data);
					}
					$rootScope.showMessage('修改密码成功');
					$state.transitionTo('member.profile')
				})
			}
		}, {force: true});
	}]);
});
